package com.twu.biblioteca;

import com.twu.biblioteca.exception.InvalidCommandException;
import com.twu.biblioteca.operation.Operation;
import com.twu.biblioteca.util.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static com.twu.biblioteca.util.CommonUtils.getInput;

class BibliotecaApp {

    private static Map<String, Operation> operations = new HashMap<String, Operation>();

    static {
        Configuration configuration = new Configuration();
        operations.put("checkbook", configuration.initCheckBookOperation());
        operations.put("returnbook", configuration.initReturnBookOperation());
        operations.put("checkmovie", configuration.initCheckMovieOperation());
        operations.put("returnmovie", configuration.initReturnMovieOperation());
        operations.put("listbooks", configuration.initListBooksOperation());
        operations.put("listmovies", configuration.initListMoviesOperation());
        operations.put("login", configuration.initLoginSystemOperation());
        operations.put("quit", configuration.initLogoutSystemOperation());
        operations.put("userinformation", configuration.initViewUserOperation());
    }

    public static void main(String[] args) {
        welcomeTo();
        while (true) {
            String command = getInput();
            String[] commands = analyseInput(command);
            Operation operation = null;
            try {
                operation = getOperation(commands[0]);
                operate(operation, commands);
            } catch (InvalidCommandException e) {
                System.err.println(e.getMessage());
            }

        }
    }

    private static String[] analyseInput(String command) {
        return command.split("\\s+");
    }


    public static Operation getOperation(String commandType) throws InvalidCommandException {
        Operation operation = operations.get(commandType.toLowerCase());
        if (operation != null) {
            return operation;
        } else {
            throw new InvalidCommandException();
        }
    }


    public static void operate(Operation operation, String[] commands) {
        commands = Arrays.copyOfRange(commands, 1, commands.length + 1);
        try {
            String result = operation.operate(commands);
            System.out.println(result);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }


    public static void welcomeTo() {
        System.out.println("*******************************************************************************");
        System.out.println("                           Welcome to Biblitoteca Library!                 ");
        System.out.println("***listbooks***listmovies");
        Operation operation = null;
        try {
            String commands[] = {"listbooks"};
            operation = getOperation(commands[0]);
            operate(operation, commands);
        } catch (InvalidCommandException e) {
            System.err.println(e.getMessage());
        }

    }
}
